import axios from 'axios';
import type { Method } from 'axios';
/**
 * @function 发起HTTP请求
 * @description 仅用于客户端环境
 * @param url 接口地址
 * @param method 请求类型
 * @param data 请求参数
 */
export function request<T>(url: string, method: Method = 'get', data = {}): () => Promise<T> {
    if (process.server) return () => Promise.resolve({} as any);
    return () => {
        return new Promise<T>((resolve, reject) => {
            axios
                .request({
                    url,
                    method,
                    params: method === 'get' ? data : {},
                    data: method === 'post' ? data : {},
                })
                .then(res => {
                    resolve(res?.data || res);
                })
                .catch(err => {
                    reject(err);
                });
        });
    };
}
export default request;
